<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师姓名" prop="teacherName">
        <el-input v-model="queryParams.teacherName" placeholder="请输入教师姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="获奖级别" prop="teachAwardLevel">
        <el-select v-model="queryParams.teachAwardLevel" placeholder="请选择获奖级别" clearable>
          <el-option v-for="dict in dict.type.teach_award_level" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="获奖等级" prop="teachAwardGrade">
        <el-select v-model="queryParams.teachAwardGrade" placeholder="请选择获奖等级" clearable>
          <el-option v-for="dict in dict.type.teach_award_grade" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker v-model="daterangeTeachAwardTime" style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.audit_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['achievements:achievements:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['achievements:achievements:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['achievements:achievements:remove']">删除</el-button>
      </el-col>
      <!-- 审核   -->
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-check" size="mini" @click="openAuditDialog" :disabled="multiple"
          v-hasPermi="['system:check']" plain>审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['achievements:achievements:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 确保visible是通过sync双向绑定 -->
    <audit-dialog :visible.sync="auditDialogVisible" url="/honors/honors/check" table_name="jw_teach_achievements"
      id_name="teach_id" ref="audit" @submit="handleAuditSubmit" />
    <el-table v-loading="loading" :data="achievementsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="teachId" />
      <el-table-column label="教师姓名" align="center" prop="teacherName" />
      <el-table-column label="教工号" align="center" prop="teachEmployeeNumber" />
      <el-table-column label="成果名称" align="center" prop="teachAchievementName" />
      <el-table-column label="奖励名称" align="center" prop="teachAwardName" />
      <el-table-column label="获奖级别" align="center" prop="teachAwardLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.teach_award_level" :value="scope.row.teachAwardLevel" />
        </template>
      </el-table-column>
      <el-table-column label="获奖等级" align="center" prop="teachAwardGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.teach_award_grade" :value="scope.row.teachAwardGrade" />
        </template>
      </el-table-column>
      <el-table-column label="授奖部门" align="center" prop="teachAwardingDepartment" />
      <el-table-column label="时间" align="center" prop="teachAwardTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.teachAwardTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="未通过原因" align="center" prop="rejectionReason" />
      <el-table-column label="团队成员" align="center" prop="temMembers" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="佐证材料" align="center" prop="wdUplocal" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['achievements:achievements:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['achievements:achievements:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改教学成果奖对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1040px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="教师姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入教师姓名" />
        </el-form-item>
        <el-form-item label="教工号" prop="teachEmployeeNumber">
          <el-input v-model="form.teachEmployeeNumber" placeholder="请输入教工号" />
        </el-form-item>
        <el-form-item label="成果名称" prop="teachAchievementName">
          <el-input v-model="form.teachAchievementName" placeholder="请输入成果名称" />
        </el-form-item>
        <el-form-item label="奖励名称" prop="teachAwardName">
          <el-input v-model="form.teachAwardName" placeholder="请输入奖励名称" />
        </el-form-item>
        <el-form-item label="获奖级别" prop="teachAwardLevel">
          <el-select v-model="form.teachAwardLevel" placeholder="请选择获奖级别">
            <el-option v-for="dict in dict.type.teach_award_level" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="获奖等级" prop="teachAwardGrade">
          <el-select v-model="form.teachAwardGrade" placeholder="请选择获奖等级">
            <el-option v-for="dict in dict.type.teach_award_grade" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="授奖部门" prop="teachAwardingDepartment">
          <el-input v-model="form.teachAwardingDepartment" placeholder="请输入授奖部门" />
        </el-form-item>
        <el-form-item label="时间" prop="teachAwardTime">
          <el-date-picker clearable v-model="form.teachAwardTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="团队成员" prop="temMembers">
          <el-input v-model="form.temMembers" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="佐证材料" prop="wdUplocal">
          <file-upload v-model="form.wdUplocal" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAchievements,
  getAchievements,
  delAchievements,
  addAchievements,
  updateAchievements
} from '@/api/achievements/achievements'
import AuditDialog from '@/components/AuditDialog/AuditDialog.vue' // 引入审核子组件

export default {
  name: 'Achievements',
  dicts: ['teach_award_grade', 'teach_award_level', 'audit_status'],
  components: { AuditDialog },
  data() {
    return {
      // 控制审核对话框的显示
      auditDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学成果奖表格数据
      achievementsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 佐证材料时间范围
      daterangeTeachAwardTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        teachAwardLevel: null,
        teachAwardGrade: null,
        teachAwardTime: null,
        auditStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 打开审核对话框
    openAuditDialog() {
      if (this.ids != null) {
        this.auditDialogVisible = true // 显示审核对话框
        this.$refs['audit'].setData(this.ids)
      } else this.$message.error('请选择要审核的信息')
    },
    // 提交审核数据
    async handleAuditSubmit() {
      this.auditDialogVisible = false // 关闭审核对话框
      await this.getList() // 刷新数据列表
    },

    /** 查询教学成果奖列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (
        this.daterangeTeachAwardTime != null &&
        this.daterangeTeachAwardTime !== ''
      ) {
        this.queryParams.params['beginTeachAwardTime'] =
          this.daterangeTeachAwardTime[0]
        this.queryParams.params['endTeachAwardTime'] =
          this.daterangeTeachAwardTime[1]
      }
      listAchievements(this.queryParams).then(response => {
        this.achievementsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        teachId: null,
        teacherName: null,
        teachEmployeeNumber: null,
        teachAchievementName: null,
        teachAwardName: null,
        teachAwardLevel: null,
        teachAwardGrade: null,
        teachAwardingDepartment: null,
        teachAwardTime: null,
        auditStatus: null,
        rejectionReason: null,
        temMembers: null,
        userId: null,
        deptId: null,
        updateTime: null,
        wdUplocal: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTeachAwardTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teachId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加教学成果奖'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const teachId = row.teachId || this.ids
      getAchievements(teachId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改教学成果奖'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.teachId != null) {
            updateAchievements(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAchievements(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teachIds = row.teachId || this.ids
      this.$modal
        .confirm('是否确认删除教学成果奖编号为"' + teachIds + '"的数据项？')
        .then(function () {
          return delAchievements(teachIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'achievements/achievements/export',
        {
          ...this.queryParams
        },
        `achievements_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
